<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/18
 * Time: 16:56
 */
namespace app\admin\model;
use think\Model;
class IntegralGoodsModel extends Model{
    /**
     * 后置商品操作
     */
    public function afterSave($goods_id){
        //商品相册
        if(!empty(input('post.goods_images'))){
            $goodsImagesArr = db('IntegralGoodsImages')->where(['goods_id'=>$goods_id])->select();
            //删除图片
            foreach($goodsImagesArr as $item){
                if(!in_array($item['image'],input('post.goods_images'))){
                    $goodsImagesDel[] = $item['id'];
                }
            }
            if(!empty($goodsImagesDel)){
                db('IntegralGoodsImages')->delete(implode(',',$goodsImagesDel));
            }

            //添加图片
            foreach($goodsImagesArr as $item){
                $goodsImages[] = $item['image'];
            }
            foreach(input('post.goods_images') as $item){
                if(!in_array($item,$goodsImages)){
                    $goodsImagesAdd[] = [
                        'goods_id' => $goods_id,
                        'image' => $item,
                    ];
                }
            }
            if(!empty($goodsImagesAdd)){
                db('IntegralGoodsImages')->saveAll($goodsImagesAdd);
            }
        }
    }
}